//
//  MessageDetailRichTextCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/9/25.
//

import UIKit
import WebKit

//typealias BlockRichTextHeight = (_ webViewHeight: CGFloat) -> Void//web高度

class MessageDetailRichTextCell: UITableViewCell {
    var webView : WKWebView!
    var webViewHeight: CGFloat = 0.01
//    var webViewHeightBlock: BlockRichTextHeight!//web高度
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .white
        self.selectionStyle = .none
        self.clipsToBounds = true
        self.initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initUI(){
        let config = WKWebViewConfiguration.init()
        let userContentController = WKUserContentController()
        config.userContentController = userContentController
        self.webView = WKWebView.init(frame: CGRect(x: 14, y: 0, width: kScreenWidth - 28, height: 270))
        self.webView.uiDelegate = self
        self.webView.navigationDelegate = self
        self.webView.autoresizesSubviews = true
        self.webView.allowsBackForwardNavigationGestures = false
        self.webView.scrollView.showsVerticalScrollIndicator = false
        self.webView.scrollView.showsHorizontalScrollIndicator = false
        self.webView.isUserInteractionEnabled = true
        self.webView.scrollView.isUserInteractionEnabled = true
        self.webView.scrollView.isScrollEnabled = false
        self.webView.sizeToFit()
        self.contentView.addSubview(self.webView)
        self.webView.scrollView.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.new, context: nil)
        
        
        self.webView.snp.makeConstraints{ make in
            make.top.equalTo(13)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(self.webViewHeight)
            make.bottom.equalTo(-10)
        }
    }
    
    
    ///监听进度条
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        ///监听webView.scrollview的大小
        if keyPath == "contentSize" {
            if self.webView.frame.size.height < self.webView.scrollView.contentSize.height {
                self.webViewHeight = self.webView.scrollView.contentSize.height
                self.webView.snp.updateConstraints { (make) in
                    make.height.equalTo(self.webViewHeight)
                }
                self.updateConstraints()
//                if self.webViewHeightBlock != nil {
//                    self.webViewHeightBlock(self.webViewHeight)
//                }
                if let table = self.superview as? UITableView {
                    table.beginUpdates()
                    table.endUpdates()
                }
            }
        }
    }
    
    
    
    func setModel(model: MessageListModel){
       
        let htmlStr = model.content ?? ""
        if htmlStr != "" && self.webViewHeight < 20{
            self.webView.loadHTMLString(XTManageCenter.getHtmlLocalScaleString(htmlStr: htmlStr), baseURL: nil)
        }
        self.updateConstraints()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension MessageDetailRichTextCell:WKUIDelegate, WKNavigationDelegate{
    func webViewUserContentController(_ scriptMessageHandlerArray: [String], didReceive message: WKScriptMessage) {
        //
    }
    
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        printLog("开始加载")
    }
    
    func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!){
        print(webView.height)
    }

}
